package com.wntms.ywa.web;

import com.github.pagehelper.PageInfo;
import com.wntms.gps.pojo.CarriageContract;
import com.wntms.gps.pojo.FreightLine;
import com.wntms.gps.service.ICarriageContractService;
import com.wntms.ywa.dao.ModifyDao;
import com.wntms.ywa.dao.TmsDao;
import com.wntms.ywa.pojo.*;
import com.wntms.ywa.service.AddService;
import com.wntms.ywa.service.FreightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/index")
//解决跨域问题
//@CrossOrigin(origins = "*")
public class IndexController {
    @Autowired
    private FreightService freightService;
    @Autowired
    private AddService addService;
    @Autowired
    private ICarriageContractService iCarriageContractService;
    //新增货运单
    @PostMapping
    public String add(@RequestBody AddNew addNew){
        Integer id;
        if( addService.findMaxId()== null){
            id=1;
        }else{
            id=addService.findMaxId().getId()+1;
        }
        addNew.setId(id);
        addService.addFreightContract(addNew);
        addService.addCustomerIn(addNew);
        addService.addCustomerInformation(addNew);
        addService.addFreightInformation(addNew);
        addService.addFreightLine(addNew);
        addService.addFreightStatus(addNew);
//同时给老伍数据传值
        t.addfreight_settle(addNew);
        addService.addC(addNew);
        return "200";
    }
    //详情
    @GetMapping("/{id}")
    public List<Details> findDetails(@PathVariable Integer id) {
        System.out.println("详情页展示成功");
        System.out.println(freightService.findDetails(id));
        return freightService.findDetails(id);
    }
//    就一次 不想写service了
    @Autowired
    private TmsDao t;
    @Autowired
    private ModifyDao m;
//    新增运输单
    @PostMapping("/addCarriage")
    public int addCarriage(@RequestBody FreightContract freightContract) throws ParseException {
        Auditstatus auditstatus=new Auditstatus();
        auditstatus.setId(freightContract.getUid());
        auditstatus.setAuditstatus("已审核");
        m.updateAuditstatus(auditstatus);
        String fid = freightContract.getFid();
//转换
        com.wntms.gps.pojo.FreightContract freightContract1 = new com.wntms.gps.pojo.FreightContract();
        Long uid = Long.valueOf(freightContract.getUid());
        Long coid = Long.valueOf(freightContract.getCoid());
        Double insurancefare = freightContract.getInsurancefare();
        Double fare = freightContract.getFare();
        Integer statusId = Integer.parseInt(freightContract.getFsid().toString());
        freightContract1.setFid(fid);
        freightContract1.setUid(uid);
        freightContract1.setCoid(coid);
        freightContract1.setInsurancefare(insurancefare);
        freightContract1.setFare(fare);
        freightContract1.setFiid(Long.valueOf(freightContract.getFiid()));
        freightContract1.setFsid(Long.valueOf(freightContract.getFsid()));

        Details time = t.findArrivetime(freightContract.getUid());
        Date arrivetime = new SimpleDateFormat("yyyy-MM-dd").parse(time.getArrivetime());

        freightContract1.setArrivetime(arrivetime);
        return iCarriageContractService.generateCarriageContract(freightContract1);
    }

    @GetMapping()
    public List<FreightContract> findIndex(){
        System.out.println("首页获取成功");
        return freightService.findIndex();
    }
    //    分页
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage , @PathVariable Integer pageSize){
        List<FreightContract> list = freightService.findByPage(currentPage,pageSize);

        Result result=new Result();
        if(list != null){
            PageInfo<FreightContract> pageInfo=new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    //    删除运单
//    方法1
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id) {
        freightService.deleteCustomerIn(id);
        freightService.deleteById(id);
        freightService.deleteFreightInformation(id);
        freightService.deleteCustomerInforation(id);
        freightService.deleteFreightLine(id);
        t.deletefse(id);
        t.deletefss(id);
        return  "200";
    }
//    方法2
    @DeleteMapping("/detele")
    public void deteleCustomer(@RequestBody FreightContract freightContract){
        freightService.deleteById(freightContract.getId());
        freightService.deleteCustomerIn(freightContract.getCoid());
        System.out.println("删除成功");
    }

}
